//
//  AddCardCheckInfoEngine.m
//  HuLaQuan
//
//  Created by hok on 1/28/16.
//  Copyright © 2016 Jianghao. All rights reserved.
//

#import "AddCardCheckInfoEngine.h"

#import "CurrentUser.h"

@implementation AddCardCheckInfoEngine

- (NSMutableDictionary *)cardInfo
{
    if (!_cardInfo) {
        _cardInfo = [[NSMutableDictionary alloc] init];
    }
    return _cardInfo;
}

- (void)getCardInfo:(NSString *)cardID name:(NSString *)name
{
    
    [self.cardInfo setObject:cardID forKey:@"cardNo"];
    [self.cardInfo setObject:name forKey:@"realName"];
    
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"getCardInfo\" sysName=\"attach\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"%@\" userId=\"%@\" channelId=\"88888888\"><cardNo>%@</cardNo><attrBitMap>11100000</attrBitMap><attach>attach</attach></msg>",[self getSerialNumber],[CurrentUser sharedInstance].token,[CurrentUser sharedInstance].ID,cardID];
    //
    NSString *randomKey = [self createKeyWithLength:24];
    
    NSLog(@"%@ -- src  ",src);
    
    
    NSString *first =[self firstString];
    
    NSString *second = [self secondStringWithKey:randomKey];
    NSString *third = [self thirdStringWithBody:src andKey:randomKey];
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",first,second,third];
    
    
    
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_CASHIER parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            if ([item isKindOfClass:[NSArray class]]) {
                NSString *resString = [_DES decrypt:[item lastObject] withKey:randomKey];
                
                NSDictionary *resultDic = [self parseXMLFromYinLianConnection:resString];
                
                NSDictionary *jsonObject;
                if ([resultDic[@"bitMapValue"] isKindOfClass:[NSString class]]) {
                    NSData *da= [resultDic[@"bitMapValue"] dataUsingEncoding:NSUTF8StringEncoding];
                    
                    NSError *error = nil;
                    
                    jsonObject = [NSJSONSerialization JSONObjectWithData:da options:NSJSONReadingAllowFragments error:&error];
                    NSLog(@"jsonObjec %@",jsonObject);
                }
                
                
                [self.cardInfo setObject:resultDic[@"bankName"] forKey:@"bankName"];
                [self.cardInfo setObject:resultDic[@"bankId"] forKey:@"bankId"];
                [self.cardInfo setObject:jsonObject forKey:@"cardInfo"];
                
                
                result = @{@"isSuccess":@YES,@"result":resultDic};
                
            }
            
        }else{
            
            result = @{@"isSuccess":@NO,@"message":item};
            NSLog(@"%@",result);
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"GetCardInfoFinish" object:result];
    }];

}


- (void)verifyCardWithPhone:(NSString *)phone identity:(NSString *)identity
{
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"authCard\" sysName=\"attach\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"%@\" userId=\"%@\" channelId=\"88888888\"><customerNm>%@</customerNm><cardNo>%@</cardNo><accType>%@</accType><certifId>%@</certifId><phoneNo>%@</phoneNo><CVN2></CVN2><expired></expired><attach>attach</attach></msg>",[self getSerialNumber],[CurrentUser sharedInstance].token,[CurrentUser sharedInstance].ID,self.cardInfo[@"realName"],self.cardInfo[@"cardNo"], self.cardInfo[@"cardInfo"][@"accType"] ,identity,phone];
    
    
    [self.cardInfo setObject:phone forKey:@"phone"];
    [self.cardInfo setObject:identity forKey:@"identity"];
    //
    NSString *randomKey = [self createKeyWithLength:24];
    
    NSLog(@"%@ -- src ---%@ key ",src,randomKey);
    
    
    NSString *first =[self firstString];
    
    NSString *second = [self secondStringWithKey:randomKey];
    NSString *third = [self thirdStringWithBody:src andKey:randomKey];
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",first,second,third];
    
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_CASHIER parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            if ([item isKindOfClass:[NSArray class]]) {
                NSString *resString = [_DES decrypt:[item lastObject] withKey:randomKey];
                
                NSDictionary *resultDic = [self parseXMLFromYinLianConnection:resString];
                
                /**
                 *  设置cardId为验证后返回的加密cardId，在addCard时调用
                 */
                [self.cardInfo setObject:resultDic[@"cardId"] forKey:@"cardId"];
                
                result = @{@"isSuccess":@YES,@"result":resultDic};
                
            }
            
        }else{
            
            result = @{@"isSuccess":@NO,@"message":item};
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"VerifyCardInfoFinish" object:result];
    }];
}
- (void)addCard:(NSString *)smsToken
{
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"addCard\" sysName=\"attach\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"%@|%@\" userId=\"%@\" channelId=\"88888888\"><cardId>%@</cardId><phoneNo>%@</phoneNo><attach>attach</attach></msg>",[self getSerialNumber],[CurrentUser sharedInstance].token,smsToken,[CurrentUser sharedInstance].ID,self.cardInfo[@"cardId"], self.cardInfo[@"phone"]];
    
    
    //
    NSString *randomKey = [self createKeyWithLength:24];
    
    NSLog(@"%@ -- src ---%@ key ",src,randomKey);
    
    
    NSString *first =[self firstString];
    
    NSString *second = [self secondStringWithKey:randomKey];
    NSString *third = [self thirdStringWithBody:src andKey:randomKey];
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",first,second,third];
    
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_CUSTOMER parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            
                NSString *resString = [_DES decrypt:[item lastObject] withKey:randomKey];
                
             
                
                result = @{@"isSuccess":@YES};
                
            
            
        }else{
            
            result = @{@"isSuccess":@NO,@"message":item};
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"AddCardFinish" object:result];
    }];
}
@end
